ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C1 Invoked Function.vbs",1).ReadAll'调用函数库
'< -----------------End of function-----------------Start Script----------------- >'
GetRequestDetails : winstr = Split(GetC1Env(UserDetail(0)), "cis.jsp", -1, 1)(0) & "uiPage"
FindC1EnvInIE(GetC1Env(UserDetail(0)))

if trim(UserDetail(2)) <>"" then '<--------------------get info from model ID'
	validUser(UserDetail(2))
	if isErrorIDFound = "true"  then
		msgbox("the model id doesn't exist or has multiple matched value")
		WScript.quit
	end if
	if UCase(UserDetail(4)) <> "Y" then
		'<----------------------------Get Model User  Portals Profile User ID
		Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
		Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
		while TabPageContent Is Nothing
			WScript.Sleep 200 : Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
		wend
		Set ModelUserPPUI = TabPageContent.document.getelementbyid("PORTAL_OVRD_USER")
		if ModelUserPPUI is nothing then
			Set TDList = ie.document.frames(0).document.getelementbyid("tabMenu").contentWindow.document.getelementsbytagname("td")
			SwitchTab("Main")
			
			Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
			Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
			while TabPageContent Is Nothing
				WScript.Sleep 200 : Set TabPageContent = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow
			wend
			Set ModelUserPPUI = TabPageContent.document.getelementbyid("PORTAL_OVRD_USER")
		end if
		
		if ModelUserPPUI.value ="" then
			ModelUserPPUI=""
		else
			ModelUserPPUI=ModelUserPPUI.value
		end if
	end if
	'<-----------------------------Change to Characteristics menu'
	set fso = createobject("scripting.filesystemobject")
	set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0) & "_Sales.txt", true)
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	Set TabMenu = ie.document.frames(0).document.getelementbyid("tabMenu")
	Set TabMenuContent = TabMenu.contentWindow
	Set TDList = TabMenuContent.document.getelementsbytagname("td")
	SwitchTab("Characteristics")
	'<---------------------Locate the Characteristics '
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	Set Characteristics= LocatePageID("USER_CHAR_GRID").document.getelementsbytagname("select")
	on error resume next
	while Characteristics Is Nothing
		WScript.Sleep 200 : Set Characteristics= LocatePageID("USER_CHAR_GRID").document.getelementsbytagname("select")
	wend
	on error goto 0
	WScript.Sleep 2000
	'<--------------------- get model ID sales list
	for each item in Characteristics
		if item.value = "SALCHN" then
			if ModelUserSaleNum < 10 then
				saleValueID = "USER_CHAR:" & Mid(item.id,11,1) & "$CHAR_VAL"
			else
				saleValueID = "USER_CHAR:" & Mid(item.id,11,2) & "$CHAR_VAL"
			end if
			ReDim Preserve ModelUserSaleList(ModelUserSaleNum)
			REM ModelUserSaleList(ModelUserSaleNum) = userCharGridContent.document.getelementbyid(saleValueID).value
			ModelUserSaleList(ModelUserSaleNum) = LocatePageID("USER_CHAR_GRID").document.getelementbyid(saleValueID).value
			file.WriteLine(ModelUserSaleList(ModelUserSaleNum))
			ModelUserSaleNum=ModelUserSaleNum+1
		end if
	Next
	file.Close : set file = nothing : set fso = nothing
	if ModelUserSaleList(0) = "NoDataHere" then'<---------------------quit script if model id doesn't have  any sales list
		if UCase(UserDetail(4)) = "Y" then
			msgbox("The model ID deosn't have any sale channel")
			WScript.quit
		end if
	end if
else '<-----------------------------------------get info form text'
	ModelUserPPUI = trim(UserDetail(3))
	WshShell.run "C1Inputs.txt",1,true
	msgbox "Please save sales channel value in the opened C1Inputs.txt file before click OK"
	LoadFile ModelUserSaleList'<-----------------------get  sale list from text if no model ID is provided
	'<-----------------------------Change to Characteristics menu'
	set fso = createobject("scripting.filesystemobject")
	set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0) & "_Sales.txt", true)
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	Set TabMenu = ie.document.frames(0).document.getelementbyid("tabMenu")
	Set TabMenuContent = TabMenu.contentWindow
	Set TDList = TabMenuContent.document.getelementsbytagname("td")
	SwitchTab("Characteristics")
end if

WshShell.run "C1Inputs.txt",1,true
msgbox "Please save C1 ID in the opened C1Inputs.txt file before click OK"
LoadFile UserIDList 
'<---------------------Search end user'
dim processNum : processNum = 1
for each user in UserIDList
	validUser(user)
	if isErrorIDFound = "true"  then '<-------- go to next user if the current one doesn't exist
		OutputErrorID user
		msgbox("the  ID doesn't exist or has multiple matched value")
		continue
	end if
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	'<-------------- --------------------------------------------------start adding sales list'
	if UCase(UserDetail(4)) = "Y" then '<----------------only add end user sale list'
		GetEndUserSaleList'<------------success here
		AddSalesList EndUserSaleList,ModelUserSaleList
	else
		if (processNum mod 2) = 1 then '<----------------------------------------------------------------------- process 次数为奇数时执行顺序'
			if ModelUserSaleList(0) <> "NoDataHere" then
				GetEndUserSaleList
				AddSalesList EndUserSaleList,ModelUserSaleList
			end if
			msgbox "1"
			'<----------------------------------switch to the main page'
			Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
			Set TDList = ie.document.frames(0).document.getelementbyid("tabMenu").contentWindow.document.getelementsbytagname("td")
			SwitchTab("Main") : UpdatePortalsProfile(ModelUserPPUI)  	'<-----------------------------Update End User Portals Profile User ID
		else '<--------------------------------------------------------------------------------------------------------- process 次数为偶数时执行顺序'
			UpdatePortalsProfile(ModelUserPPUI) 	'<-----------------------------Update End User Portals Profile User ID
			if ModelUserSaleList(0) <> "NoDataHere" then
				'<----------------------------------switch to the Characteristics  page'
				Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
				Set TDList = ie.document.frames(0).document.getelementbyid("tabMenu").contentWindow.document.getelementsbytagname("td")
				SwitchTab("Characteristics") : GetEndUserSaleList : AddSalesList EndUserSaleList,ModelUserSaleList
			end if
		end if
	end if
	ie.document.frames(0).document.getelementbyid("IM_SAVE").click
	Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
	WScript.Sleep 5000
	processNum = processNum + 1
next
msgbox "Script finished successfully!"
